package com.lc.common.util;

import cn.hutool.core.util.StrUtil;
import com.lc.common.bean.response.LoginInfo;
import com.lc.common.component.AbstractStorage;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * ThreadLocalUtil
 * <p>
 * 用于获取当前线程中存放的数据
 *
 * @author ch
 */
@SuppressWarnings("unused")
public class UserInfo extends AbstractStorage {

    private UserInfo() {
    }

    /**
     * 获取登陆者信息
     *
     * @return 登陆者信息  {@link LoginInfo}
     */
    public static LoginInfo get() {
        return THREAD_LOCAL.get();
    }

    /**
     * 获取当前登陆者的 用户名称
     * Redundant suppression
     *
     * @return 当前登陆者的名称
     */
    @SuppressWarnings("unused")
    public static String getUserName() {
        LoginInfo loginInfo = get();
        return loginInfo == null ? null : loginInfo.getName();
    }

    /**
     * 获取当前登陆者的id
     *
     * @return 当前登陆id
     */
    @SuppressWarnings("unused")
    public static String getUserId() {
        LoginInfo loginInfo = get();
        return loginInfo == null ? null : loginInfo.getUserId();
    }

    /**
     * 获取当前登陆者编号
     *
     * @return 当前登陆者编号
     */
    @SuppressWarnings("unused")
    public static String getPersonNo() {
        LoginInfo loginInfo = get();
        return loginInfo == null ? null : loginInfo.getPersonNo();
    }

    public static List<String> getDepartmentPower(String departmentNo) {
        LoginInfo loginInfo = get();
        List<String> departmentNos = new ArrayList<>();
        if (loginInfo != null) {
            List<String> departmentNoPower = loginInfo.getDepartmentNoPower();
            if (departmentNoPower != null && !departmentNoPower.isEmpty()) {
                if (StrUtil.isBlank(departmentNo)) {
                    return departmentNoPower;
                } else {
                    List<String> collect = departmentNoPower.stream().filter(x -> x.startsWith(departmentNo) || departmentNo.startsWith(x)).collect(Collectors.toList());
                    if (!collect.isEmpty()) {
                        return collect;
                    }
                }
            }
        }
        departmentNos.add("NOTHING");
        return departmentNos;
    }
}
